package com.wwcom.darteagle.view;

import com.wwcom.darteagle.R;

import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Rect;
import android.graphics.drawable.Drawable;
import android.util.AttributeSet;
import android.view.View;

public class MatchStatsPositionMarkerView extends View {
	private int numInList;
	private int posInList;
	private Drawable inactiveMarker;
	private Drawable activeMarker;
	private Rect inactiveMarkerRect;
	private Rect layoutRect;
	private static int MAX_DISPLAY= 10;
	
	//constructors
	public MatchStatsPositionMarkerView(Context context) {
		super(context);
		initialiseSelf();
	}

	public MatchStatsPositionMarkerView(Context context, AttributeSet attrs) {
		super(context,attrs);
		initialiseSelf();
	}

	private void initialiseSelf() {
		inactiveMarker = this.getResources().getDrawable(R.drawable.inactive_marker);
		inactiveMarkerRect = new Rect(0, 0, inactiveMarker.getIntrinsicWidth(), inactiveMarker.getIntrinsicHeight());
		layoutRect= inactiveMarkerRect;
		activeMarker = this.getResources().getDrawable(R.drawable.active_marker);
		
	}
	
	//methods
	@Override
	protected void onDraw (Canvas canvas) {
		
		int width= this.getWidth();
		int height= this.getHeight();
		
		//deal with when there are more dots than can be displayed
		int numInDisplayList= numInList;
		int posInDisplayList= posInList;
		if (numInList>MAX_DISPLAY) {
			numInDisplayList= MAX_DISPLAY;
			//truncate aronud visible position
			if (posInList > MAX_DISPLAY) {
				if (posInList < numInList-1) {
					posInDisplayList= numInDisplayList-2;
				} else {
					posInDisplayList= numInDisplayList-1;					
				}
			}
		}
		
		int graphicSize= inactiveMarkerRect.width();
        int totalWidth= graphicSize*2*numInDisplayList;
                	
        
		int centerWidth= width/2;
		int yPos= height/2 - inactiveMarkerRect.height()/2;
		int startX= centerWidth - totalWidth/2;
				
        for (int i= 0; i<numInDisplayList; i++) {
        	layoutRect.offsetTo(startX, yPos);
        	if (i==posInDisplayList) {
        		activeMarker.setBounds(layoutRect);
        		activeMarker.draw(canvas);        		
        	} else {
        		inactiveMarker.setBounds(layoutRect);
        		inactiveMarker.draw(canvas);
        	}
        	startX+= graphicSize*2;
        }
	}

	public void setNumInList(int numInList) {
		this.numInList = numInList;
	}

	public void setPosInList(int posInList) {
		this.posInList = posInList;
	}	

}
